package com.ruoyi.crm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.crm.domain.BusinessOpportunity;
import com.ruoyi.crm.domain.CustomerInfo;
import com.ruoyi.crm.domain.StageEntity;
import com.ruoyi.crm.service.ICustomerService;
import com.ruoyi.crm.service.IStageService;
import com.ruoyi.system.api.RemoteUserService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.crm.domain.vo.CrmProspectInfoVo;
import com.ruoyi.crm.domain.bo.CrmProspectInfoBo;
import com.ruoyi.crm.service.ICrmProspectInfoService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 商机管理控制器
 * 前端访问路由地址为:/crm/prospectInfo
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/prospectInfo")
public class CrmProspectInfoController extends BaseController {

    private final ICrmProspectInfoService iCrmProspectInfoService;
    private final IStageService stageService;
    private final ICustomerService customerService;
    @DubboReference
    private RemoteUserService remoteUserService;

    /**
     * 查询商机管理列表
     */
    @SaCheckPermission("crm:prospectInfo:list")
    @GetMapping("/list")
    public TableDataInfo<CrmProspectInfoVo> list(CrmProspectInfoBo bo, PageQuery pageQuery) {
        TableDataInfo<CrmProspectInfoVo> list=iCrmProspectInfoService.selectProspectPageList(bo,pageQuery);
        list.getRows().forEach(P->{
            if(P.getCustomerId()!=null){
                CustomerInfo customerInfo=customerService.selectCustomerById(P.getCustomerId());
                if (customerInfo != null) {
                    P.getParams().put("customerName",customerInfo.getCustomerName());
                }

            }
            if (P.getUserId()!=null){
                String sysUser=remoteUserService.selectUserNameById(P.getUserId());
                if (sysUser!=null) {
                    P.getParams().put("userName",sysUser);
                }
            }
        });
        return list;
    }

    /**
     * 导出商机管理列表
     */
    @SaCheckPermission("crm:prospectInfo:export")
    @Log(title = "商机管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CrmProspectInfoBo bo, HttpServletResponse response) {
        List<CrmProspectInfoVo> list = iCrmProspectInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "商机管理", CrmProspectInfoVo.class, response);
    }

    /**
     * 获取商机管理详细信息
     *
     * @param prospectId 主键
     */
    @SaCheckPermission("crm:prospectInfo:query")
    @GetMapping("/{prospectId}")
    public R<CrmProspectInfoVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long prospectId) {
        return R.ok(iCrmProspectInfoService.queryById(prospectId));
    }

    /**
     * 新增商机管理
     */
    @SaCheckPermission("crm:prospectInfo:add")
    @Log(title = "商机管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CrmProspectInfoBo bo) {
        return toAjax(iCrmProspectInfoService.insertByBo(bo));
    }

    /**
     * 修改商机管理
     */
    @SaCheckPermission("crm:prospectInfo:edit")
    @Log(title = "商机管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CrmProspectInfoBo bo) {
        return toAjax(iCrmProspectInfoService.updateByBo(bo));
    }

    /**
     * 删除商机管理
     *
     * @param prospectIds 主键串
     */
    @SaCheckPermission("crm:prospectInfo:remove")
    @Log(title = "商机管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prospectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] prospectIds) {
        return toAjax(iCrmProspectInfoService.deleteWithValidByIds(Arrays.asList(prospectIds), true));
    }
}
